package com.dw.dwdzcommerce.base.activity;

import java.util.Random;

import com.dw.dwdzcommerce.tools.commons.FileUtil;
import com.dw.dwdzcommerce.tools.commons.L;
import com.dw.dwdzcommerce.tools.commons.bitmap.BitmapUtil;
import com.dw.dwdzcommerce.tools.commons.bitmap.ImageSizeUtil;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;

/**
 * Created by zz on 15/11/26.
 */
public abstract class BasePickImageActivity extends BaseActivity {
    protected static final int REQUESTCODE_CODE_CROP_IMAGE = 201;
    protected static final int REQUESTCODE_CODE_PICK_IMAGE = 200;

    protected abstract ImageView bindCropImageViewDisplayContainer();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUESTCODE_CODE_PICK_IMAGE) {
            if (data == null) {
                return;
            }
            Uri pathUri = data.getData();
            String path = pathUri.getPath();
            L.i(BasePickImageActivity.class, "path" + path);
            if (!isCrop()) {
                Cursor cursor = this.getContentResolver().query(pathUri, null, null, null, null);
                int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                path = cursor.getString(index);
                ImageView imageView = bindCropImageViewDisplayContainer();
                Bitmap bitmap = null;
                if (imageView != null) {
                    ImageSizeUtil.ImageSize imageViewSize = ImageSizeUtil.getImageViewSize(imageView);
                    bitmap = BitmapUtil.decodeSampledBtmapFromPath(path, imageViewSize);
                } else {
                    bitmap = BitmapFactory.decodeFile(path);
                }
                onPick(bitmap, path);
                return;
            }
            cropBitmap(pathUri);
        }

        if (requestCode == REQUESTCODE_CODE_CROP_IMAGE) {
            getPathAndSaveCrop(data);

        }

    }


    private void getPathAndSaveCrop(Intent data) {
        // TODO Auto-generated method stub
        if (data != null) {
            String end = ".jpg";
            Bundle extras = data.getExtras();
            String cachePath = this.getCacheDir().toString();

            String picPath = cachePath + "/" + "supper_class_avator_cache"+new Random().nextInt(100000) + end;
            if (extras != null) {
                Bitmap cropBitmap = extras.getParcelable("data");
                // Uri uri =
                // Uri.parse(MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
                // cropBitmap, null, null));//这句话是要将这张图片加到图片库里面
                new FileUtil(this).saveBitmap(picPath, cropBitmap);
                onCrop(cropBitmap, picPath);
            }

    }

}


    public void pickImage() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUESTCODE_CODE_PICK_IMAGE);
    }

    /**
     * 剪裁
     *
     * @param uri
     */
    private void cropBitmap(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUESTCODE_CODE_CROP_IMAGE);
    }


    /**
     * @param bm
     * @return
     */
    protected void onPick(Bitmap bm, String path) {

    }

    protected boolean isCrop() {
        return false;
    }

    protected void onCrop(Bitmap cropBitmap, String savePath) {

    }

}
